package com.accompany.service.impl;

import com.accompany.entity.User;
import com.accompany.entity.Wallet;
import com.accompany.dao.WalletMapper;
import com.accompany.service.WalletService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 钱包表 服务实现类
 * </p>
 *
 * @author DC
 * @since 2024-12-26
 */
@Slf4j
@Service
public class WalletServiceImpl extends ServiceImpl<WalletMapper, Wallet> implements WalletService {

    @Autowired
    private WalletMapper walletMapper;

    /**
     * 分页查询钱包列表
     *
     * @param page 页码
     * @param size 每页大小
     * @return 分页结果
     */
    @Override
    public Page<Wallet> getPaginatedWallets(Integer page, Integer size) {
        try {
            log.info("分页查询钱包列表，页码：{}, 每页大小：{}", page, size);
            Page<Wallet> pageObj = new Page<>(page, size);
            Page<Wallet> walletPage = baseMapper.selectPage(pageObj, null);
            walletPage.getRecords().forEach(item -> log.info(item.toString()));
            return walletPage;
        } catch (Exception e) {
            log.error("分页查询钱包列表时发生错误，页码：{}, 每页大小：{}", page, size, e);
            throw e;
        }
    }
    /**
     * 获取所有钱包列表
     *
     * @return 钱包列表
     */
    @Override
    public List<Wallet> getAllWallets() {
        try {
            log.info("获取所有钱包列表");
            return walletMapper.selectList(null);
        } catch (Exception e) {
            log.error("获取所有钱包列表时发生错误", e);
            throw e;
        }
    }

    /**
     * 获取单个钱包信息
     *
     * @param id 钱包ID
     * @return 钱包对象
     */
    @Override
    public Wallet getWalletById(Long id) {
        try {
            log.info("获取单个钱包信息，钱包ID：{}", id);
            return walletMapper.selectById(id);
        } catch (Exception e) {
            log.error("获取单个钱包信息时发生错误，钱包ID：{}", id, e);
            throw e;
        }
    }

    /**
     * 更新钱包信息
     *
     * @param wallet 钱包信息
     */
    @Override
    @Transactional
    public void updateWallet(Wallet wallet) {
        try {
            log.info("更新钱包信息，钱包信息：{}", wallet.toString());
            walletMapper.updateById(wallet);
        } catch (Exception e) {
            log.error("更新钱包信息时发生错误，钱包信息：{}", wallet.toString(), e);
            throw e;
        }
    }

    /**
     * 删除钱包
     *
     * @param id 钱包ID
     */
    @Override
    @Transactional
    public void deleteWallet(Long id) {
        try {
            log.info("删除钱包，钱包ID：{}", id);
            walletMapper.deleteById(id);
        } catch (Exception e) {
            log.error("删除钱包时发生错误，钱包ID：{}", id, e);
            throw e;
        }
    }

    /**
     * 创建钱包
     *
     * @param wallet 钱包信息
     */
    @Override
    @Transactional
    public void createWallet(Wallet wallet) {
        try {
            log.info("创建钱包：{}", wallet.toString());
            walletMapper.insert(wallet);
        } catch (Exception e) {
            log.error("创建钱包时发生错误，钱包信息：{}", wallet.toString(), e);
            throw e;
        }
    }
}